package online.chenke.juc;

/**
 * 两个线程按顺序打印A1B2C3...
 *
 */
public class Test002 {
	volatile static boolean isStart = false;
	
	public static void main(String[] args) {
		Object obj = new Object();
		char[] arr1 = "12345".toCharArray();
		char[] arr2 = "ABCDEF".toCharArray();
		
		new Thread(()->{
			try {
				synchronized (obj) {
					for (int i = 0; i < arr2.length; i++) {
						isStart = true;
						System.out.print(arr2[i]);
						obj.notify();
						obj.wait();
					}
					obj.notify();
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}, "t1").start();
		
		new Thread(()->{
			try {
				synchronized (obj) {
					while(!isStart) {
						obj.wait();
					}
					
					for (int i = 0; i < arr1.length; i++) {
						System.out.print(arr1[i]);
						obj.notify();
						obj.wait();
					}
					
					obj.notify();
				}
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}, "t2").start();
	}
}
